<template>
    <a-drawer
            :title="title"
            :width="800"
            placement="right"
            :closable="false"
            @close="close"
            :visible="visible"
    >

        <a-spin :spinning="confirmLoading">
            <a-form :form="form">

                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="id（主键）">
                    <a-input placeholder="请输入id（主键）" v-decorator="['cid', validatorRules.cid ]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="合同编号">
                    <a-input placeholder="请输入合同编号" v-decorator="['contractid', validatorRules.contractid ]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="合同名称">
                    <a-input placeholder="请输入合同名称" v-decorator="['contractname', validatorRules.contractname ]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="说明">
                    <a-input placeholder="请输入说明" v-decorator="['remarks', {}]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="负责人">
                    <a-input placeholder="请输入负责人" v-decorator="['person', validatorRules.person ]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="供应商">
                    <a-input placeholder="请输入供应商" v-decorator="['supplier', validatorRules.supplier ]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="采购合同">
                    <a-input placeholder="请输入采购合同" v-decorator="['avatarfile', {}]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="合同生效日期">
                    <a-date-picker v-decorator="[ 'effectivetime', validatorRules.effectivetime ]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="合同失效日期">
                    <a-date-picker v-decorator="[ 'endtime', validatorRules.endtime ]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="审核人1">
                    <a-input placeholder="请输入审核人1" v-decorator="['auditor1', {}]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="审核人2">
                    <a-input placeholder="请输入审核人2" v-decorator="['auditor2', {}]"/>
                </a-form-item>
                <a-form-item
                        :labelCol="labelCol"
                        :wrapperCol="wrapperCol"
                        label="审核人3">
                    <a-input placeholder="请输入审核人3" v-decorator="['auditor3', {}]"/>
                </a-form-item>

            </a-form>
        </a-spin>
        <a-button type="primary" @click="handleOk">确定</a-button>
        <a-button type="primary" @click="handleCancel">取消</a-button>
    </a-drawer>
</template>

<script>
    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import moment from "moment"

    export default {
        name: "Sys_contractModal",
        data() {
            return {
                title: "操作",
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                form: this.$form.createForm(this),
                validatorRules: {
                    cid: {rules: [{required: true, message: '请输入id（主键）!'}]},
                    contractid: {rules: [{required: true, message: '请输入合同编号!'}]},
                    contractname: {rules: [{required: true, message: '请输入合同名称!'}]},
                    person: {rules: [{required: true, message: '请输入负责人!'}]},
                    supplier: {rules: [{required: true, message: '请输入供应商!'}]},
                    effectivetime: {rules: [{required: true, message: '请输入合同生效日期!'}]},
                    endtime: {rules: [{required: true, message: '请输入合同失效日期!'}]},
                },
                url: {
                    add: "/contract/sys_contract/add",
                    edit: "/contract/sys_contract/edit",
                },
            }
        },
        created() {
        },
        methods: {
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'cid', 'contractid', 'contractname', 'remarks', 'person', 'supplier', 'avatarfile', 'auditor1', 'auditor2', 'auditor3'))
                    //时间格式化
                    this.form.setFieldsValue({effectivetime: this.model.effectivetime ? moment(this.model.effectivetime) : null})
                    this.form.setFieldsValue({endtime: this.model.endtime ? moment(this.model.endtime) : null})
                });

            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        //时间格式化
                        formData.effectivetime = formData.effectivetime ? formData.effectivetime.format() : null;
                        formData.endtime = formData.endtime ? formData.endtime.format() : null;

                        console.log(formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })


                    }
                })
            },
            handleCancel() {
                this.close()
            },


        }
    }
</script>

<style lang="less" scoped>
    /** Button按钮间距 */
    .ant-btn {
        margin-left: 30px;
        margin-bottom: 30px;
        float: right;
    }
</style>